$(document).ready(function () {
    findStudent()
    updateMark()
});

//获取updateMark.html的url地址参数
function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        //return unescape(r[2]);
        //decodeURI解码
        return decodeURI(decodeURI(r[2]));
    }
    return null;
}

//查找所有学生及成绩
function findStudent() {
    // 获取FrontPageAddUpdateMark页面超链接的参数 --这样调用：
    var termYear = getQueryString("termYear");
    var termNum = getQueryString("termNum");
    var courseType = getQueryString("courseType");
    var courseName = getQueryString("courseName");
    var courseId = getQueryString("courseNum");
    var url = "http://localhost:8080/mark/selectMark?termYear=" + termYear + "&termNum=" + termNum + "&courseType=" + courseType +
        "&courseName=" + courseName + "&courseId=" + courseId;
    $('#dataTable').dataTable({
        "bDeferRender": true,
        "sAjaxDataProp": "data.markList",
        "sAjaxSource": url,
        "sPaginationType": "bs_full", //"bs_normal", "bs_two_button", "bs_four_button", "bs_full"
        "fnPreDrawCallback": function (oSettings) {
            $('.dataTables_filter input').addClass('form-control input-large').attr('placeholder', 'Search');
            $('.dataTables_length select').addClass('form-control input-small');
        },
        "oLanguage": {
            "sSearch": "",
            "sLengthMenu": "<span>_MENU_ entries</span>",
            "sZeroRecords": "暂无相关信息",
        },
        "bJQueryUI": false,
        "bAutoWidth": false,
        "sDom": "<'row'<'col-lg-6 col-md-6 col-sm-12 text-center'l><'col-lg-6 col-md-6 col-sm-12 text-center'f>r>t<'row-'<'col-lg-6 col-md-6 col-sm-12'i><'col-lg-6 col-md-6 col-sm-12'p>>",
        "aoColumnDefs": [
            {
                "mDataProp": null,
                "sDefaultContent": "<input type='text' class='updateMark' name='updateMark'/>",
                "aTargets": [-1]
            }
        ],
        "aoColumns": [
            {"mDataProp": "markId"},
            {"mDataProp": "stuNum"},
            {"mDataProp": "stuName"},
            {"mDataProp": "mark"},
            {"mDataProp": ""},
        ],
    })

}

//修改成绩
function updateMark() {
    $(document).on('click', '#updateMarkBtn', function () {
        var termYear = getQueryString("termYear");
        var termNum = getQueryString("termNum");
        var courseType = getQueryString("courseType");
        var courseName = getQueryString("courseName");
        var courseId = getQueryString("courseNum");
        var updateMark = $(".updateMark");


        for (var i = 0; i < updateMark.length; i++) {
            var mark = $(updateMark[i]).val();//更新的成绩，原始成绩不获取
            var markId = $(updateMark[i]).parents("tr").children(0).eq(0).text();
            var stuNum = $(updateMark[i]).parents("tr").children(0).eq(1).text();
            var stuName = $(updateMark[i]).parents("tr").children(0).eq(2).text();
            // alert("-markId-"+markId+"-stuNum-"+stuNum+"-stuName-"+stuName+"-updateMark-"+mark)
            //  alert($(updateMark[i]).val()+"hhhh")
            var reg = new RegExp("^(\\d|[1-9]\\d|100)$");
            var stuName1 = $(updateMark[i]).parents("tr").children(0).eq(2).text();



            if (mark != "" && mark != null &&reg.test(mark)) {

                $.ajax({
                    url: "http://localhost:8080/mark/updateMark?termYear=" + termYear + "&termNum=" + termNum +
                    "&courseType=" + courseType + "&courseName=" + courseName +
                    "&mark=" + mark + "&stuNum=" + stuNum + "&stuName=" + stuName +
                    "$courseId=" + courseId + "&markId=" + markId + "&mark=" + mark,
                    type: "GET",
                    async: false,
                    dataType: "json",
                    success: function (data) {
                        layer.alert("修改成功!");
                        //location.href='FrontPageAddUpdateMark.html';
                      //  reloadPage()
                    },
                    error: function () {
                        layer.alert("你的网络太慢了！添加失败!再试一次吧！");
                    }
                });
            }else if(!reg.test(mark)&&mark != "" && mark != null){
                layer.alert(stuName+"修改不合法!");
            }


        }
    })


    //刷新页面
    function reloadPage() {
        window.location.reload();
    }
}